//validation ajax functions
function ajaxSubmit(form){
	$(form).find('.showText').html('<img id="loading-img" src="images/ajax-loader.gif" />').show();
	//$body = (window.opera) ? (document.compatMode == "CSS1Compat" ? $('html') : $('body')) : $('html,body');
	//$body.animate( { scrollTop: $(form).offset().top }, 600 );
	$.post($(form).attr('action'), $(form).serialize(), function(data){
		if(data['success'] == '1'){
			if(data['msg']) {
				$(form).find('.showText').show().html('<p class="valideText">'+data['msg']+'</p><a class="close-green"></a>');
			}
			if(data['url']){
				setTimeout(function(){
					location.href=data['url'];
				},1000);
			}
		}else if(data['success'] == '0'){
			$(form).find('.showText').hide().html('\
			<p class="errorText">'+data['msg']+'</p>\
			<a class="close-red"></a>').fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);
		}
	},"json");
	return false;
}
function ajaxSubmit_invite(form){
	$(form).find('.showText').html('<img id="loading-img" src="images/ajax-loader.gif" />').show();
	$body = (window.opera) ? (document.compatMode == "CSS1Compat" ? $('html') : $('body')) : $('html,body');
	$body.animate( { scrollTop: $(form).prev('.title').offset().top }, 1000 );
	$.post($(form).attr('action'), $(form).serialize(), function(data){
		if(data['success'] == '1'){
				alert(data['error_emails']+'<br>'+data['already_sent']+'<br>'+data['will_send']);
			if(data['url']){
				setTimeout(function(){
					location.href=data['url'];
				},500);
			}
		}else if(data['success'] == '0'){
			$(form).find('.showText').hide().html('\
			<p class="errorText">'+data['msg']+'</p>\
			<a class="close-red"></a>').fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);
		}
	},"json");
	return false;
}

$().ready(function(){
    //form_select function
		$(".select_value").each(function(){
			val = $(this).val();
			if(val != "") {
				text = $(this).closest(".form_select").find(".select_option[val='"+val+"']").text();
        if(text != "")
				$(this).closest(".form_select").find(".select_show").text(text);
			}
		});
    $(".wedding_form .form_select .select_show").click(function(){
        $(this).siblings(".select_options").slideToggle("fast");
        $(this).toggleClass("select_show_on");
    });
    $(".wedding_form .form_select .select_option").click(function(){
        text = $(this).text();
        val = $(this).attr("val");
        $(this).closest(".select_options").siblings(".select_show").text(text).removeClass("select_show_on");
        $(this).closest(".select_options").siblings(".select_value").val(val);
        $(this).closest(".select_options").slideUp("fast");
    });
	$(".select_options").mouseleave(function(){
		$(this).slideUp("fast");
		$(this).removeClass("select_show_on");
	});
    
    //form_radio function
    $(".wedding_form .form_radio .radio_option").click(function(){
        val = $(this).attr("val");
        $(this).closest(".form_radio").find(".radio_icon").removeClass("radio_icon_on");
        $(this).find(".radio_icon").addClass("radio_icon_on");
        $(this).closest(".form_radio").find(".radio_value").val(val);
    });
    
    //form_checkbox function
		$(".checkbox_value").each(function(){
			val = $(this).val();
			if(val == "1") {
				$(this).closest(".form_checkbox").find('.checkbox_icon').addClass('checkbox_icon_on');
			} else {
				$(this).closest(".form_checkbox").find('.checkbox_icon').removeClass('checkbox_icon_on');
			}
		});
    $(".form_checkbox .checkbox_option").click(function(){
        if($(this).find(".checkbox_icon").hasClass("checkbox_icon_on")) {
            $(this).find(".checkbox_icon").removeClass("checkbox_icon_on");
            $(this).siblings(".checkbox_value").val("0");
        } else {
            $(this).find(".checkbox_icon").addClass("checkbox_icon_on");
            $(this).siblings(".checkbox_value").val("1");
        }
    });

	//deconnect function
	$("#click-drop").click(function(){
		$("#bien-deconnect").slideToggle(400);
	});
	$(".welcome .deconnect").click(function(){
		var sure;
		sure = confirm("Are you sure to disconnect?");
		if(sure == true)
			$.ajax({
				url:"inc/disconnect.php",
				success:function(){window.location.reload()}
			});
	});
	
	//success or error message
	$(".close-green, .close-red").live("click",function(){
		$(this).closest(".showText").fadeOut("fast");
	});
	
	//form validation
	jQuery.validator.addMethod("lettersonly", function(value, element) {
		return this.optional(element) || /^[a-zA-Zéèçàù -]+$/i.test(value);
	}, "");
	$("#login").validate({
		errorElement: "em",
		rules: {
			email: {
				required: true,
				email: true
			},
			password:{
				required: true,
				minlength: 5,
				maxlength: 20
			}
		},
		messages: {
			email: {
				required: "Ce champ est obligatoire",
				email: "L'adresse e-maill n'est pas valide"
			},
			password: {
				required: "Ce champ est obligatoire",
				minlength: "5 caractères minimum",
				maxlength: "20 caractères maximum"
			}
		},
		submitHandler:function(form){
			ajaxSubmit(form);
		}
	});
	$("#subscribe").validate({
		errorElement: "em",
		rules: {
			gender: {
				required: true
			},
			firstName: {
				required: true,
				lettersonly: true
			},
			lastName: {
				required: true,
				lettersonly: true
			},
			email: {
				required: true,
				email: true
			},
			password:{
				required: true,
				minlength: 5,
				maxlength: 20
			}
		},
		messages: {
			gender: {
				required: "Ce champ est obligatoire"
			},
			firstName: {
				required: "Ce champ est obligatoire",
				lettersonly: "Le nom est pas validé"
			},
			lastName: {
				required: "Ce champ est obligatoire",
				lettersonly: "Le prénom est pas validé"
			},
			email: {
				required: "Ce champ est obligatoire",
				email: "L'adresse e-maill n'est pas valide"
			},
			password: {
				required: "Ce champ est obligatoire",
				minlength: "5 caractères minimum",
				maxlength: "20 caractères maximum"
			}
		},
		submitHandler:function(form){
			ajaxSubmit(form);
		}
	});
	$("#les-maries-1, #les-maries-2, #compte-1").validate({
		errorElement: "em",
		rules: {
			gender: {
				required: true
			},
			firstName: {
				required: true,
				lettersonly: true
			},
			lastName: {
				required: true,
				lettersonly: true
			},
			email: {
				required: true,
				email: true
			},
			phoneNumber: {
				digits: true,
				minlength: 10,
				remote: {
					url: "check-phone.php",
					type: "post",
					data: {
						phoneNumber: function() {
							return $("#phoneNumber").val();
						}
					}
				}
			},
			yyyy: {
				required: true,
				remote: {
					url: "check-date.php",
					type: "post",
					data: {
						dd: function() {
							return $("#dd").val();
						},
						mm: function() {
							return $("#mm").val();
						},
						yyyy: function() {
							return $("#yyyy").val();
						}
					}
				}
			},
		},
		messages: {
			gender: {
				required: "Ce champ est obligatoire"
			},
			firstName: {
				required: "Ce champ est obligatoire",
				lettersonly: "Le nom est pas validé"
			},
			lastName: {
				required: "Ce champ est obligatoire",
				lettersonly: "Le prénom est pas validé"
			},
			email: {
				required: "Ce champ est obligatoire",
				email: "L'adresse e-maill n'est pas valide"
			},
			phoneNumber: {
				digits: "Veuillez renseigner que des chiffres",
				minlength: "Le numéro doit avoir 10 chiffres",
				remote: "Le téléphone doit commencer par 06 ou 07"
			},
			yyyy: {
				required: "Veuillez renseigner votre date de naissance",
				remote: "La date n'est pas correcte"
			},
		},
		submitHandler:function(form){
			ajaxSubmit(form);
		}
	});
	$("#compte-2").validate({
		errorElement: "em",
		rules: {
			oldcode: {
				required: true,
				minlength: 5,
				maxlength: 20
			},
			newcode: {
				required: true,
				minlength: 5,
				maxlength: 20
			},
			'con-newcode': {
				required: true,
				minlength: 5,
				maxlength: 20,
				equalTo: "#newcode"
			}
		},
		messages: {
			oldcode: {
				required: "Ce champ est obligatoire",
				minlength: "5 caractères minimum",
				maxlength: "20 caractères maximum"
			},
			newcode: {
				required: "Ce champ est obligatoire",
				minlength: "5 caractères minimum",
				maxlength: "20 caractères maximum"
			},
			'con-newcode': {
				required: "Ce champ est obligatoire",
				minlength: "5 caractères minimum",
				maxlength: "20 caractères maximum",
				equalTo: "Le mot de passe ne correspond pas"
			}
		},
		submitHandler:function(form){
			ajaxSubmit(form);
		}
	});
	$("#les-maries-3").validate({
		errorElement: "em",
		rules: {
			yyyy1: {
				required: true,
				remote: {
					url: "check-date.php",
					type: "post",
					data: {
						dd: function() {
							return $("#dd1").val();
						},
						mm: function() {
							return $("#mm1").val();
						},
						yyyy: function() {
							return $("#yyyy1").val();
						}
					}
				}
			},
			yyyy2: {
				required: true,
				remote: {
					url: "check-date.php",
					type: "post",
					data: {
						dd: function() {
							return $("#dd2").val();
						},
						mm: function() {
							return $("#mm2").val();
						},
						yyyy: function() {
							return $("#yyyy2").val();
						}
					}
				}
			},
			yyyy3: {
				required: true,
				remote: {
					url: "check-date.php",
					type: "post",
					data: {
						dd: function() {
							return $("#dd3").val();
						},
						mm: function() {
							return $("#mm3").val();
						},
						yyyy: function() {
							return $("#yyyy3").val();
						}
					}
				}
			}
		},
		messages: {
			yyyy1: {
				required: "Veuillez renseigner votre date de naissance",
				remote: "La date n'est pas correcte"
			},
			yyyy2: {
				required: "Veuillez renseigner votre date de naissance",
				remote: "La date n'est pas correcte"
			},
			yyyy3: {
				required: "Veuillez renseigner votre date de naissance",
				remote: "La date n'est pas correcte"
			}
		},
		submitHandler:function(form){
			ajaxSubmit(form);
		}
	});
	$("#grandjour-1").validate({
		errorElement: "em",
		rules: {
			event_name: {
				required: true
			},
			event_titre: {
				required: true
			},
			hh: {
				required: true
			},
			mn: {
				required: true
			},
			adresse: {
				required: true
			},
			code: {
				required: true,
				digits: true,
				minlength: 5
			},
			ville: {
				required: true,
				lettersonly: true
			},
			pays: {
				required: true
			},
			voiture: {
				required: true
			},
			train: {
				required: true
			},
			avion: {
				required: true
			}
		},
		messages: {
			event_name: {
				required: "Ce champ est obligatore"
			},
			event_titre: {
				required: "Ce champ est obligatore"
			},
			hh: {
				required: "Ce champ est obligatore"
			},
			mn: {
				required: "Ce champ est obligatore"
			},
			adresse: {
				required: "Veuillez renseigner votre adresse"
			},
			code: {
				required: "Veuillez renseigner votre code postal",
				digits: "Veuillez renseigner que des chiffres",
				minlength: "Le code postal doit avoir 5 chiffres"
			},
			ville: {
				required: "Veuillez renseigner votre ville",
				lettersonly: "Seules les lettres sont autorisées"
			},
			pays: {
				required: "Ce champ est obligatore"
			},
			voiture: {
				required: "Ce champ est obligatore"
			},
			train: {
				required: "Ce champ est obligatore"
			},
			avion: {
				required: "Ce champ est obligatore"
			}
		},
		submitHandler:function(form){
			ajaxSubmit(form);
		}
	});
	$("#histoire-1").validate({
		errorElement: "em",
		submitHandler:function(form){
			ajaxSubmit(form);
		}
	});
	$("#oublie-mdp").validate({
		errorElement: "em",
		rules: {
			email: {
				required: true,
				email: true
			}
		},
		messages: {
			email: {
				required: "Ce champ est obligatoire",
				email: "L'adresse e-maill n'est pas valide"
			}
		},
		submitHandler:function(form){
			ajaxSubmit(form);
		}
	});
	$("#send-invitation").validate({
		errorElement: "em",
		rules: {
			'send-addresse': {
				required: true
			}
		},
		messages: {
			'send-addresse': {
				required: "Ce champ est obligatoire"
			}
		},
		submitHandler:function(form){
			ajaxSubmit(form);
		}
	});
})
